﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using System.Collections;


namespace logicunivesityinventorystore
{
    public partial class Change : System.Web.UI.Page
    {
        EmployeeFacade ef = new EmployeeFacade();
        CollectionPointFacade cf = new CollectionPointFacade();
        DepartmentFacade df = new DepartmentFacade();
        static BusinessLogicLayer.Department department = new BusinessLogicLayer.Department();
        BusinessLogicLayer.Employee employee = new BusinessLogicLayer.Employee();
        BusinessLogicLayer.Employee representative = new BusinessLogicLayer.Employee();
        CollectionPoint collectionpoint = new BusinessLogicLayer.CollectionPoint();
      
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string empid = User.Identity.Name;// Session["UserName"].ToString();
                employee = ef.FindEmployeeByID(empid);
                department = df.FindDepartmentByDepartmentID(employee.Departmentcode);
                collectionpoint = cf.FindCollectionById(department.CollectionPointID);
                representative = ef.FindEmployeeByID(department.RepresentativeID);

                txtContactName.Text = department.ContactName.ToString();
                txtContactNo.Text = department.TelephoneNo.ToString();
                txtFax.Text = department.FaxNo.ToString();
                lblRep.Text = representative.EmployeeName.ToString();
                lblCollection.Text = collectionpoint.Location.ToString() + " " + collectionpoint.Time.TimeOfDay.ToString();
                string id = "EngSch";
                TimeSpan t = cf.GetTime(id);
                lblCollectionTime.Text = t.ToString();

                ddlRepName.DataSource = df.SearchEmployees(empid);
                ddlRepName.DataTextField = "EmployeeName";
                ddlRepName.DataValueField = "EmployeeID";
                ddlRepName.DataBind();

            }

        }

        protected void ddlCollection_SelectedIndexChanged(object sender, EventArgs e)
        {
            string collectionId = ddlCollection.SelectedValue;
            if (collectionId == null)
                collectionId = "EngSch";
            TimeSpan t = cf.GetTime(collectionId);
            lblCollectionTime.Text = t.ToString();

            ddlCollection.SelectedIndex=Int32.Parse(collectionpoint.CollectionPointID.ToString())-1;
           
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            department.ContactName = txtContactName.Text;
            department.RepresentativeID = ddlRepName.SelectedValue.ToString();
            department.CollectionPointID = ddlCollection.SelectedValue.ToString();

            df.UpdateContactName(department.DepartmentCode, department.ContactName);
            df.UpdateRepresentativeRole(department.DepartmentCode, department.RepresentativeID);
            df.UpdateCollectionID(department.DepartmentCode,department.CollectionPointID);

            
              Response.Redirect("HomePage_Department.aspx");
            
        }

        
    }
}